process.env.NODE_ENV ||= 'development';

import { ValidationPipe } from '@nestjs/common';
import { ConfigService, ConfigType } from '@nestjs/config';
import { NestFactory } from '@nestjs/core';

import { AppModule } from './app.module';
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
// import { JwtAuthGuard } from './common/guards/jwt-auth.guard';
import { ResponseInterceptor } from './common/interceptors/response.interceptor';

import appConfig from './config/app.config';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const configService = app.get(ConfigService);
  const appConfigTyped: ConfigType<typeof appConfig> | undefined = configService.get('app');

  // Combines both groups to form the global prefix
  const globalPrefix = `${appConfigTyped!.routeRootGroupName}${appConfigTyped!.routeApiGroupName}`;
  app.setGlobalPrefix(globalPrefix);
  Reflect.set(global, 'nest:global_prefix', globalPrefix);

  // Enable global validation pipe
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true, // Strips properties that don't have decorators
      forbidNonWhitelisted: true, // Throws error if extra properties found
      transform: true, // Automatically transforms payloads to DTO instances
    }),
  );

  app.useGlobalFilters(new AllExceptionsFilter());
  // app.useGlobalGuards(new JwtAuthGuard());
  app.useGlobalInterceptors(new ResponseInterceptor());

  await app.listen(process.env.PORT ?? 3000);
}

bootstrap();
